package com.dexilaonian.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.activity.OnlineVolunteerActivity;
import com.dexilaonian.bean.TopicBean;
import com.dexilaonian.bean.VolunteerBean;
import com.dexilaonian.utils.ImageLoader;
import java.util.List;

/**
 * Created by Administrator on 2018/6/28.
 */

public class VolunteerAdapter extends RecyclerView.Adapter<VolunteerAdapter.ViewHolder> {

    private MyItemClickListener mItemClickListener;
    private final Context mContext;
    private final List<VolunteerBean> mVolunteerList;

    public VolunteerAdapter(Context context, List<VolunteerBean> volunteerlist) {
        mContext = context;
        mVolunteerList = volunteerlist;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_volunteer, null);
        //将全局的监听传递给holder
        ViewHolder holder = new ViewHolder(view, mItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mVolunteerList.size() != 0) {
            holder.rl_ask2.setVisibility(View.VISIBLE);
            String content = mVolunteerList.get(position).getContent();
            String imageUrl = mVolunteerList.get(position).getImageUrl();
            int isUser = mVolunteerList.get(position).getIsUser();
            if (isUser == 1) {  //判断是客户发的消息还是系统回复的信息
                holder.rl_ask2.setVisibility(View.VISIBLE);
                holder.rlhuifu.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(content)) {  //判断是否有内容
                    holder.tv_voluntee_ask2.setText(content);
                } else {
                    holder.tv_voluntee_ask2.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(imageUrl)) {
                    ImageLoader.display(mContext, imageUrl, holder.vol_ask2);
                } else {
                    holder.vol_ask2.setVisibility(View.GONE);
                }
            } else {
                holder.rlhuifu.setVisibility(View.VISIBLE);
                holder.rl_ask2.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(content)) {
                    holder.tvVol_answer.setText(content);
                } else {
                    holder.tvVol_answer.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(imageUrl)) {
                    ImageLoader.display(mContext, imageUrl, holder.iv_voluneer_pic);
                } else {
                    holder.iv_voluneer_pic.setVisibility(View.GONE);
                }
            }

        }

    }

    @Override
    public int getItemCount() {
            return mVolunteerList==null? 0:mVolunteerList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        MyItemClickListener mListener;
        TextView tv_voluntee_ask2;
        ImageView vol_ask2;
        RelativeLayout rl_ask2;

        TextView tvVol_answer;
        ImageView iv_voluneer_pic;
        RelativeLayout rlhuifu;

        public ViewHolder(View itemView, MyItemClickListener myItemClickListener) {
            super(itemView);
            this.mListener = myItemClickListener;
            itemView.setOnClickListener(this);

            tv_voluntee_ask2 = (TextView) itemView.findViewById(R.id.voluntee_ask2);
           vol_ask2 = (ImageView) itemView.findViewById(R.id.iv_voluneer_ask2);
            rl_ask2 = (RelativeLayout) itemView.findViewById(R.id.rl_ask2);

            rlhuifu = (RelativeLayout) itemView.findViewById(R.id.rl_huifu);
            tvVol_answer = (TextView) itemView.findViewById(R.id.tv_voluntee_answer);
            iv_voluneer_pic = (ImageView) itemView.findViewById(R.id.iv_voluneer_answer);
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(v, getPosition());
            }

        }
    }

    /**
     * 创建一个回调接口
     */
    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
